﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;

public partial class RegisterForm : System.Web.UI.Page
{
    string strUploadImagesPath = System.Configuration.ConfigurationManager.AppSettings["UploadImagesPath"];
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            btnRegister.Attributes.Add("onclick", "return Confirm()");
            btnCheckID.Attributes.Add("onclick", "return CheckUserID()");

            if (!IsPostBack)
            {
                Binding.BindShopType(ddlShopType, true, "0", "โปรดระบุ");
                Binding.BindPackage(ddlPackage, true, "0", "โปรดระบุ");
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = "<br>" + ex.Message + "<br><br>";
        }
    }

    protected void btnRegister_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable dt = new DataTable();
            DBConn oDBConn = new DBConn();
            oDBConn.OpenConnection();

            SqlCommand oCommand = new SqlCommand("sp_Deal_User_SELECTBYID", oDBConn.DBConnection);
            oCommand.CommandType = CommandType.StoredProcedure;
            oCommand.Parameters.Add("@USER_ID", SqlDbType.VarChar).Value = txtUserID.Text.Trim();

            SqlDataAdapter oDataAdapter = new SqlDataAdapter(oCommand);
            oDataAdapter.Fill(dt);
            oDataAdapter = null;

            bool blnValidUser = false;
            if (dt.Rows.Count == 0)
            {
                lblCheckID.Text = "รหัสผู้ใช้นี้ สามารถเพิ่มข้อมูลได้";
                blnValidUser = true;
            }
            else
            {
                lblCheckID.Text = "รหัสผู้ใช้นี้ ซ้ำกับในระบบ";
                blnValidUser = false;
            }
            //Upload Image
            bool blnValidUploadLogo = false;
            bool blnValidUploadImage = false;
            Int32 intLimitImagesSize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["LimitImagesSizeUploadKb"]);
            if (flUploadShopLogo.HasFile)
            {
                string strExtension = Global.GetFileExtension(flUploadShopLogo.FileName).ToLower();
                if (strExtension.Contains(".gif") || strExtension.Contains(".jpg") || strExtension.Contains(".jpeg") || strExtension.Contains(".png"))
                {
                    //File Size Upload
                    if (flUploadShopLogo.FileBytes.Length > 1024 * intLimitImagesSize)
                    {
                        lblMessage.Text = "<br>ขนาดไฟล์โลโก้ร้านค้าเกิน " + intLimitImagesSize + " KB<br>";
                        blnValidUploadLogo = false;
                    }
                    else
                    {
                        blnValidUploadLogo = true;
                    }
                }
                else
                {
                    lblMessage.Text = "<br>ประเภทไฟล์โลโก้ร้านค้าไม่ถูกต้อง<br>";
                    blnValidUploadLogo = false;
                }
            }
            else
            {
                blnValidUploadLogo = true;
            }

            if (flUploadShopImage.HasFile)
            {
                string strExtension = Global.GetFileExtension(flUploadShopImage.FileName).ToLower();
                if (strExtension.Contains(".gif") || strExtension.Contains(".jpg") || strExtension.Contains(".jpeg") || strExtension.Contains(".png"))
                {
                    //File Size Upload
                    if (flUploadShopImage.FileBytes.Length > 1024 * intLimitImagesSize)
                    {
                        lblMessage.Text = "<br>ขนาดไฟล์รูปภาพร้านค้าเกิน " + intLimitImagesSize + " KB<br>";
                        blnValidUploadImage = false;
                    }
                    else
                    {
                        blnValidUploadImage = true;
                    }
                }
                else
                {
                    lblMessage.Text = "<br>ประเภทไฟล์รูปภาพร้านค้าไม่ถูกต้อง<br>";
                    blnValidUploadImage = false;
                }
            }
            else
            {
                blnValidUploadImage = true;
            }
            //insert shop and user
            if (blnValidUser)
            {
                if (blnValidUploadLogo && blnValidUploadImage)
                {
                    string strPassword = Security.CipherCode(txtPassword.Text.Trim());
                    oCommand = new SqlCommand("sp_Deal_Shop_INSERT", oDBConn.DBConnection);
                    oCommand.CommandType = CommandType.StoredProcedure;
                    oCommand.Parameters.Add("@Shop_Name", SqlDbType.VarChar).Value = txtShopName.Text.Trim();
                    oCommand.Parameters.Add("@Shop_Address", SqlDbType.VarChar).Value = txtAddress.Text.Trim();
                    oCommand.Parameters.Add("@Shop_Lat", SqlDbType.Float).Value = txtLatitude.Text.Trim();
                    oCommand.Parameters.Add("@Shop_Long", SqlDbType.Float).Value = txtLongitude.Text.Trim();
                    oCommand.Parameters.Add("@Shop_Detail", SqlDbType.VarChar).Value = txtShopDetail.Text.Trim();
                    oCommand.Parameters.Add("@Shop_Package_ID", SqlDbType.Int).Value = ddlPackage.SelectedValue;
                    oCommand.Parameters.Add("@Shop_ShopType_ID", SqlDbType.Int).Value = ddlShopType.SelectedValue;
                    oCommand.Parameters.Add("@Shop_CreateBy", SqlDbType.VarChar).Value = txtUserID.Text.Trim();
                    Int32 LastShopID = Convert.ToInt32(oCommand.ExecuteScalar());


                    string ShopID = LastShopID.ToString();
                    string activeDir = strUploadImagesPath;
                    //*** Create Sub Folder ***//
                    DirectoryInfo DirInfo = new DirectoryInfo(Server.MapPath(activeDir + "/" + ShopID));
                    if (!DirInfo.Exists)
                    {
                        DirInfo.Create();
                    }

                    string imgPath = strUploadImagesPath + ShopID;
                    string pathHtmlLogo = "";
                    if (flUploadShopLogo.HasFile)
                    {
                        flUploadShopLogo.SaveAs(Server.MapPath(imgPath + "/" + flUploadShopLogo.FileName));
                        pathHtmlLogo = "http://www.poximo.com/images/shopimages/" + ShopID + "/" + flUploadShopLogo.FileName;
                    }
                    else
                    {
                        pathHtmlLogo = "";
                    }
                    string pathHtmlImage = "";
                    if (flUploadShopImage.HasFile)
                    {
                        flUploadShopImage.SaveAs(Server.MapPath(imgPath + "/" + flUploadShopImage.FileName));
                        pathHtmlImage = "http://www.poximo.com/images/shopimages/" + ShopID + "/" + flUploadShopImage.FileName;
                    }
                    else
                    {
                        pathHtmlImage = "";
                    }
                    oCommand = new SqlCommand("sp_Deal_Shop_UPDATEPATHIMAGE", oDBConn.DBConnection);
                    oCommand.CommandType = CommandType.StoredProcedure;
                    oCommand.Parameters.Add("@Shop_ID", SqlDbType.BigInt).Value = ShopID;
                    oCommand.Parameters.Add("@Shop_Logo", SqlDbType.VarChar).Value = pathHtmlLogo;
                    oCommand.Parameters.Add("@Shop_Image", SqlDbType.VarChar).Value = pathHtmlImage;
                    oCommand.ExecuteNonQuery();

                    oCommand = new SqlCommand("sp_Deal_User_INSERT", oDBConn.DBConnection);
                    oCommand.CommandType = CommandType.StoredProcedure;
                    oCommand.Parameters.Add("@User_ID", SqlDbType.VarChar).Value = txtUserID.Text.Trim();
                    oCommand.Parameters.Add("@User_Password", SqlDbType.VarChar).Value = strPassword;
                    oCommand.Parameters.Add("@User_FirstName", SqlDbType.VarChar).Value = txtName.Text.Trim();
                    oCommand.Parameters.Add("@User_LastName", SqlDbType.VarChar).Value = txtSurname.Text.Trim();
                    oCommand.Parameters.Add("@User_Email", SqlDbType.VarChar).Value = txtEmail.Text.Trim();
                    oCommand.Parameters.Add("@User_TelNo", SqlDbType.VarChar).Value = txtTelNo.Text.Trim();
                    oCommand.Parameters.Add("@User_UserType_ID", SqlDbType.Int).Value = 2;
                    oCommand.Parameters.Add("@User_Status", SqlDbType.Bit).Value = 1;
                    oCommand.Parameters.Add("@User_Shop_ID", SqlDbType.BigInt).Value = LastShopID;
                    oCommand.Parameters.Add("@User_CreateBy", SqlDbType.VarChar).Value = txtUserID.Text.Trim();
                    //**UserStatus 0 = False(inactive) 1 = True(active)**//

                    oCommand.ExecuteNonQuery();

                    oDBConn.Disconnect();

                    Session["USER_USERID"] = txtUserID.Text.Trim();
                    Session["USER_FULLNAME"] = txtName.Text.Trim() + " " + txtSurname.Text.Trim();
                    Session["USER_TYPE"] = 2;
                    Session["SHOP_ID"] = ShopID;

                    Response.Redirect("../shop/Shop.aspx");
                }
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message; //lblMessage.BackColor = System.Drawing.ColorTranslator.FromHtml("#EE3F44");
        }
    }

    protected void btnCheckID_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable dt = new DataTable();
            DBConn oDBConn = new DBConn();
            oDBConn.OpenConnection();

            SqlCommand oCommand = new SqlCommand("sp_Deal_User_SELECTBYID", oDBConn.DBConnection);
            oCommand.CommandType = CommandType.StoredProcedure;
            oCommand.Parameters.Add("@USER_ID", SqlDbType.VarChar).Value = txtUserID.Text.Trim();

            SqlDataAdapter oDataAdapter = new SqlDataAdapter(oCommand);
            oDataAdapter.Fill(dt);
            oDataAdapter = null;
            oDBConn.Disconnect();

            if (dt.Rows.Count == 0)
            {
                lblCheckID.Text = "รหัสผู้ใช้นี้ สามารถเพิ่มข้อมูลได้";
            }
            else
            {
                lblCheckID.Text = "รหัสผู้ใช้นี้ ซ้ำกับในระบบ";
            }
        }

        catch (Exception ex)
        {
            lblMessage.Text = ex.Message; //lblMessage.BackColor = System.Drawing.ColorTranslator.FromHtml("#EE3F44");
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtUserID.Text = "";
        txtEmail.Text = "";
        txtPassword.Text = "";
        txtConfirmPassword.Text = "";
        txtName.Text = "";
        txtSurname.Text = "";
        txtTelNo.Text = "";
        txtShopName.Text = "";
        txtAddress.Text = "";
        txtLatitude.Text = "";
        txtLongitude.Text = "";
        txtShopDetail.Text = "";
        ddlPackage.SelectedValue="0";
        ddlShopType.SelectedValue="0";
    }
}
